package refect.annotionAndReflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestAnnotationReflect {

    @MyTest
    public void test1() {
        System.out.println("--test1---");
    }

    public void test2() {
        System.out.println("--test2---");
    }

    @MyTest
    public void test3() {
        System.out.println("--test3---");
    }

    @MyTest
    public void test4() {
        System.out.println("--test4---");
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        // 模拟运行在方法上加了MyTest注解的方法
        Class<TestAnnotationReflect> c1 = TestAnnotationReflect.class;
        TestAnnotationReflect t = new TestAnnotationReflect();

        Method[] declaredMethods = c1.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            if(declaredMethod.isAnnotationPresent(MyTest.class)) {
                declaredMethod.setAccessible(true);
                declaredMethod.invoke(t);
            }
        }
    }
}
